#ifndef __case_json_kfjiweur89u83r33e3e__
#define __case_json_kfjiweur89u83r33e3e__

#include <cstddef>
#include <string>
#include <typeinfo>

// 日志组件
#include <hude/base/log.hpp>
// Json组件
#include <hude/utils/json.hpp>

#include "test/test.hpp"

namespace test
{
    using namespace hude;
  
    /*********************************************************************
     * @brief json分桢器测试
     */
    struct case_json_t : public hude::test::case_t
    {
        struct test_json_t
        {
            std::string json;
            bool result;
            size_t begin, end;
        };

        /*********************************************************************
         * @brief case_json_t
         */
        case_json_t() {}

        /*********************************************************************
         * @brief run
         */
        bool run() override
        {
            using itr_t = std::string::const_iterator;
            using checker_t = hude::utils::_details::json_checker_t< itr_t >;

            static const test_json_t strings[] =
            {
                { R"( "abc"  )", false, 1, 2 },
                { R"( 0.45  )", false, 1, 2 },
                { R"( -0.45  )", false, 1, 2 },
                { R"( false  )", false, 1, 2 },
                { R"( true  )", false, 1, 2 },
                { R"( "aa\"a\\a", {}  )", false, 1, 6 },
                { R"( {   "key": -2.2 }{}  )", true, 1, 4 },
                { R"( [ 0, 0  ]  )", true, 1, 2 },
                { R"( [ [ "aa", true, { "k":[]} ], 0  ][]  )", true, 1, 4 },
                { R"( [["aa",false,{"k":[]}],0][]  )", true, 1, 4 },
                { R"( {   "key": { "kk"  :"abc" } }  )", true, 1, 2 },
                { R"( {   "key": { "kk"  :"abc" }, "tt" : false }  )", true, 1, 2 },
                { R"( {   "key": [ 0, -0 ], "tt" : false }  )", true, 1, 2 },
                { R"( {   "key": [ "s1", "s2" ], "tt" : false }  )", true, 1, 2 },
                { R"( {   "key": [ { "s1": "s2" }, { "s1": "s2" } ], "tt" : false }  )", true, 1, 2 },
            };

            checker_t checker;

            for( size_t i = 0; i < 10000U; ++i )
            {
                for( const auto& s : strings )
                {
                    itr_t vb = s.json.cbegin();
                    itr_t ve = s.json.cend();

                    itr_t h = ve;
                    itr_t b = vb;
                    itr_t e = ve;

                    auto r = checker( h, b, e );


                    if( r != s.result )
                    {
                        if( s.result )
                        {
                            if( !r || h != vb + s.begin || b != ve - s.end )
                            {
                                llog_err( "<json>", "ERROR: checker( %s ), b( %d:%d ), e( %d:%d )",
                                    s.json.c_str(), h - vb, s.begin, b - vb, ve - s.end );
                            }
                        } else {
                                llog_err( "<json>", "ERROR: checker( %s ) = true", s.json.c_str() );
                        }

                        return false;
                    }
                }
            }

            return true;
        }
    
    };

    //--------------------------------------------------------------------------------
    TEST_CASE_ADD( "case-json", case_json_t, false );

} // namespace test

#endif //__case_json_kfjiweur89u83r33e3e__


